#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long LL;
const int MAXN=2e5+5;
int n,q,c,a[MAXN];
vector<int> G[MAXN],col[20];
vector<pair<int,int> > Q[MAXN];
int idx[20],rk[20],deg[20];
bool cmp(int x,int y) {return deg[x]>deg[y];}
int fa[MAXN],siz[MAXN],s[MAXN],top;
int fin(int x) {return fa[x]==x?x:fin(fa[x]);}
void uni(int x,int y) {
	x=fin(x),y=fin(y);
	if(x!=y) {
		if(siz[x]>siz[y]) swap(x,y);
		fa[x]=y,siz[y]+=siz[x],s[++top]=x;
	}
}
void clear(int rec) {
	while(top>rec) {
		int x=s[top--];
		siz[fa[x]]-=siz[x],fa[x]=x;
	}
}
int ans[MAXN];
void dfs(int dep,int S) {
	if(dep==c) {
		for(auto pii:Q[S]) {
			ans[pii.second]=siz[fin(pii.first)];
		}return;
	}
	int rec=top;
	for(auto i:col[dep]) {
		for(auto j:G[i]) {
			if((S>>a[j]&1)||a[j]==dep) uni(i,j);
		}
	}
	dfs(dep+1,S|1<<dep);
	clear(rec);
	dfs(dep+1,S);
}
signed main() {
	freopen("tree.in","r",stdin);
	freopen("tree.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>q>>c;
	for(int i=1;i<=n;++i) cin>>a[i],fa[i]=i,siz[i]=1;
	for(int i=2;i<=n;++i) {
		int x;cin>>x;
		G[i].emplace_back(x);
		G[x].emplace_back(i);
	}
	for(int i=1;i<=n;++i) deg[a[i]]+=G[i].size();
	for(int i=0;i<c;++i) idx[i]=i;
	sort(idx,idx+c,cmp);
	for(int i=0;i<c;++i) rk[idx[i]]=i;
	for(int i=1;i<=n;++i) {
		a[i]=rk[a[i]];
		col[a[i]].emplace_back(i);
	}
	for(int i=1;i<=q;++i) {
		int S,T=0,x;
		cin>>S>>x;
		for(int j=0;j<c;++j) T|=(S>>j&1)<<rk[j];
		Q[T].emplace_back(make_pair(x,i));
	}
	dfs(0,0);
	for(int i=1;i<=q;++i) cout<<ans[i]<<endl;
	return 0;
}